package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.ad.AdData;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.view.AdActivity;
import com.zhongxi.guoyuan.view.PlayActivity;
import com.zhongxi.guoyuan.view.beautifulStore.ProjectDetailActivity;
import com.zhongxi.guoyuan.view.beautifulStore.StoreDetailActivity;
import com.zhongxi.guoyuan.view.rewardCoin.RewardCoinDetailActivity;
import com.zhongxi.guoyuan.view.rewardCoin.RewardCoinShoperAtivity;
import com.zhongxi.guoyuan.view.user.gift.GiftDetailActivity;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * Created by Administrator on 2016/10/14.
 */

public class ImageQrAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<String> imgBannerList;

    public ImageQrAdapter(Context context, List<String> bannerList) {
        this.mContext = context;
        this.imgBannerList = bannerList;
        this.mInflater = LayoutInflater.from(context);
    }

    public void replaceAll(List<String> bannerList) {
        if (bannerList == null)
            return;
        if (imgBannerList == null) {
            imgBannerList = new ArrayList<>(bannerList);
        } else {
            imgBannerList.clear();
            imgBannerList.addAll(bannerList);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    /**
     * 实际数据Size
     *
     * @return
     */
    public int size() {
        return imgBannerList != null ? imgBannerList.size() : 1;
    }

    @Override
    public String getItem(int arg0) {
        return imgBannerList != null ? imgBannerList.get(arg0 % size()) : null;
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(final int arg0, View arg1, ViewGroup arg2) {
        try {
            ViewHolder holder = null;
            if (arg1 == null) {
                holder = new ViewHolder();
                arg1 = mInflater.inflate(R.layout.adapter_image_item, arg2, false);
                holder.ivImg = (ImageView) arg1.findViewById(R.id.ivImg);
                arg1.setTag(holder);
            } else {
                holder = (ViewHolder) arg1.getTag();
            }
            if (imgBannerList == null || imgBannerList.size() == 0) {
                holder.ivImg.setImageResource(R.mipmap.iv_loading);
            } else {
                final String mVpInfo = getItem(arg0);
                Glide.with(mContext).load(Url.IMG_URL + mVpInfo).placeholder(R.mipmap.iv_loading).fitCenter().into(holder.ivImg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return arg1;
    }

    class ViewHolder {
        private ImageView ivImg;
    }

}